package codeine;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import codeine.configuration.Links;
import codeine.configuration.directory.PeersListJson;
import codeine.executer.Task;
import codeine.mongo.IAlertsDatabaseConnector;
import codeine.utils.HttpUtils;

import com.google.gson.Gson;

public class ServerPeersListUpdater implements Task {

	public static final long SLEEP_TIME = TimeUnit.SECONDS.toMillis(5);
	private Gson gson = new Gson();
	@Inject
	private PeersListRegistry peersListRegistry;
	@Inject
	private Links links;
	@Inject
	private NodesManagerServer nodesManagerServer;
	@Inject
	private IAlertsDatabaseConnector databaseConnector;

	@Override
	public void run() {
		String json = HttpUtils.doGET(links.directoryPeerStatus());
		PeersListJson peers = gson.fromJson(json, PeersListJson.class);
		databaseConnector.getPeers(peers);
		peersListRegistry.updatePeersList(peers);
		nodesManagerServer.updateAllNodes(peers);
	}

}
